using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

using AIFSDataAccessLayer;
using AIFSContentManagementSystem;
using AIFSCMSDataAccessLayer.EntityMappers;
using AIFSContentManagementSystem.Criteria;
using AIFSCMSDataAccessLayer.ProxyModels;

namespace AIFSCMSDataAccessLayer.RepositoryImpl
{
    public class CriteriaRepository : ICriteriaRepository
    {
        IDataAwareRepositoryMap dataAwareRepositoryMap;
        public IRepositoryMap RepositoryMap
        {
            get
            {
                return dataAwareRepositoryMap;
            }
        }

        private IDataExecutionContext DataExecutionContext
        {
            get { return dataAwareRepositoryMap.DataExecutionContext; }
        }

        public CriteriaRepository(IDataAwareRepositoryMap RepositoryMap)
        {
            this.dataAwareRepositoryMap = RepositoryMap;
        }

        public Criterion GetCriterion(int? criteriaId)
        {
            DalSqlCommand cmd = this.DataExecutionContext.CreateStoredProcCommand("GetCriteria");

            cmd.CreateParameter("CRITERIA_ID", criteriaId);

            DataTable dt = this.DataExecutionContext.ExecuteDataTable(cmd);
            
            if (dt == null || dt.Rows.Count == 0)
                return null;

            //Return a proxy to support lazy loading
            CriteriaMapper mapper = new CriteriaMapper();
            CriterionProxy proxy = mapper.Map(dt.Rows[0]);
            proxy.RepositoryMap = this.RepositoryMap;
            return proxy;
        }

        public List<ExpressionElement> GetExpressionElementForCriterion(int? criteriaId)
        {
            List<ExpressionElement> elements = new List<ExpressionElement>();

            DalSqlCommand cmd = this.DataExecutionContext.CreateStoredProcCommand("GetExpression");

            cmd.CreateNullParameter("EXPRESSION_ID");
            cmd.CreateParameter("CRITERIA_ID", criteriaId);

            DataTable dt = this.DataExecutionContext.ExecuteDataTable(cmd);

            if (dt == null || dt.Rows.Count == 0)
                return elements;

            ExpressionElementMapper mapper = new ExpressionElementMapper();
            elements = mapper.Map(dt);

            return elements;
        }

        public ExpressionElement GetExpressionElement(int? criterionExpressionId)
        {
            DalSqlCommand cmd = this.DataExecutionContext.CreateStoredProcCommand("GetExpression");

            cmd.CreateParameter("EXPRESSION_ID", criterionExpressionId);
            cmd.CreateNullParameter("CRITERIA_ID");

            DataTable dt = this.DataExecutionContext.ExecuteDataTable(cmd);

            if (dt == null || dt.Rows.Count == 0)
                return null;

            ExpressionElementMapper mapper = new ExpressionElementMapper();
            ExpressionElement element = mapper.Map(dt.Rows[0]);

            return element;
        }
    }
}
